package com.sx.ems.controller.sys;


import com.sx.ems.entity.SysRole;
import com.sx.ems.service.SysRoleMenuService;
import com.sx.ems.service.SysRoleService;
import com.sx.ems.utils.MsgBean;
import com.sx.ems.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sys/role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 角色列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:role:list")
    public MsgBean list(@RequestParam Map<String, Object> params){
        PageUtils page = sysRoleService.queryRolePage(params);
        return MsgBean.ok().put("page", page);
    }

    /**
     * 角色列表
     */
    @RequestMapping("/select")
    @RequiresPermissions("sys:role:select")
    public MsgBean select(){
        List<SysRole> list = sysRoleService.list(null);
        return MsgBean.ok().put("list", list);
    }

    /**
     * 角色信息
     */
    @RequestMapping("/info/{roleId}")
    @RequiresPermissions("sys:role:info")
    public MsgBean info(@PathVariable("roleId") Long roleId){
        SysRole role = sysRoleService.getById(roleId);

        //查询角色对应的菜单
        List<Long> menuIdList = sysRoleMenuService.queryMenuIdList(roleId);

        role.setMenuIdList(menuIdList);

        return MsgBean.ok().put("role", role);
    }

    /**
     * 保存角色
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:role:save")
    public MsgBean save(@RequestBody SysRole role){
        //TODO 验证表单
        sysRoleService.saveRole(role);
        return MsgBean.ok();
    }

    /**
     * 修改角色
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:role:update")
    public MsgBean update(@RequestBody SysRole role){
        //TODO 验证表单
        sysRoleService.updateRole(role);
        return MsgBean.ok();
    }

    /**
     * 删除角色
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:role:delete")
    public MsgBean delete(@RequestBody Long[] roleIds){
        sysRoleService.removeByIds(Arrays.asList(roleIds));

        return MsgBean.ok();
    }
}

